package cn.admin.service.impl;

import cn.admin.service.PayPhotoUploadService;
import cn.admin.tools.Base64Convert;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 18:26 2019/3/23
 */
@Service
public class PayPhotoUploadServiceImpl implements PayPhotoUploadService {

    @Override
    public boolean saveImg(String data, HttpServletRequest request) {
        try {
            String path=request.getSession().getServletContext().getRealPath("img");    //得到文件虚拟路径
            File file=new File(path);
            //文件不存在则创建
            if (!file.exists()){
                System.out.println("文件不存在,正在创建");
                file.mkdir();
            }else{
                boolean directory = file.isDirectory();
                //判断是否是目录
                if (directory==true){
                    //得到该目录下所有图片
                    File[] files = file.listFiles();
                    //遍历删除
                    for (File file1 : files) {
                        synchronized (file1){
                            boolean delete = file1.delete();
                            System.out.println("是否删除成功:"+delete);
                        }
                    }
                }
            }
            boolean b = Base64Convert.base64ToImage(data, path +File.separator+ "pay.jpg" );
            return b;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }



}
